//
//  WSLoadMoreView.m
//  WSFramework
//
//  Created by zhangcong on 2018/4/17.
//  Copyright © 2018年 zhangcong. All rights reserved.
//

#import "WSLoadMoreView.h"
#import <WSConst.h>

@implementation WSLoadMoreView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    [self addSubview:self.loadMoreButton];
    
    self.activity.x = (WScreenWidth - 222) / 2;
    [self addSubview:self.activity];
    
    self.loadLabel.x = CGRectGetMaxX(self.activity.frame) + 2;
    [self addSubview:self.loadLabel];
}

-(void) startLoadMore;
{
    [self startAnimating];
    [_loadLabel setText:@"正在加载..."];
    [_loadLabel setHidden:NO];
}

- (void)endLoadMoreNoText
{
    [self endLoadMore];
    [_loadLabel setHidden:YES];
    
}

-(void) errorLoadMore
{
    [self stopAnimating];
    [_loadLabel setText:@"加载更多"];
    [_loadMoreButton setEnabled:YES];
}

-(void) endLoadMore
{
    [self stopAnimating];
    [_loadLabel setText:@""];
}

-(void) notLoadMore
{
    [self stopAnimating];
    [_loadLabel setText:@"没有更多内容啦..."];
}

-(void) startAnimating
{
    [_activity setHidden:NO];
    [_activity startAnimating];
    [_loadMoreButton setEnabled:NO];
}

-(void) stopAnimating
{
    [_activity setHidden:YES];
    [_activity stopAnimating];
    [_loadMoreButton setEnabled:NO];
}

- (void)didLoadMore:(id)sender {
    if( [self.delegate respondsToSelector:@selector(didStartLoadMoreView:)] ) {
        [(id<WSLoadMoreViewDelegate>)self.delegate didStartLoadMoreView:self];
    }
}

#pragma mark - private
- (UIActivityIndicatorView *)activity {
    if (_activity == nil) {
        _activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
        _activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    }
    return _activity;
}

- (UIButton *)loadMoreButton {
    if (_loadMoreButton == nil) {
        _loadMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loadMoreButton setFrame:CGRectMake(10, 5, WScreenWidth - 20, 30)];;
        [_loadMoreButton addTarget:self action:@selector(didLoadMore:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loadMoreButton;
}

- (UILabel *)loadLabel {
    if (_loadLabel == nil) {
        _loadLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 200, 20)];
        _loadLabel.text = @"加载数据中...";
        _loadLabel.font = [UIFont systemFontOfSize:14];
        _loadLabel.textColor = kUIColorWithRGB(0x969696);
    }
    return _loadLabel;
}

@end
